package com.ruoyi.crop.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.crop.api.domain.CheckReport;
import com.ruoyi.crop.service.CheckReportService;
import com.ruoyi.crop.service.GoodsService;
import com.ruoyi.crop.service.MaterialLotService;
import com.ruoyi.crop.util.UploadMinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/report")
public class CheckReportController extends BaseController {

    @Autowired
    CheckReportService checkReportService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    MaterialLotService materialLotService;

    /**
     * 查询检测报告信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CheckReport checkReport)
    {
        startPage();
        List<CheckReport> list = checkReportService.selectCheckReportList(checkReport);
        return getDataTable(list);
    }


    /**
     * 随机生成编号
     * @return
     */
    @GetMapping("selectCheckReportCode")
    public AjaxResult selectCheckReportCode(){
        String s = IdUtil.getSnowflake().nextIdStr();
        return success(s);
    }

    /**
     * 获取检测报告信息详细信息
     */
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Integer reportId)
    {
        return success(checkReportService.getById(reportId));
    }

    /**
     * 新增检测报告信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody CheckReport checkReport)
    {
        if (!checkReportService.checkMaterialCodeUnique(checkReport)){
            return error("此批次号已被使用");
        }
        checkReport.setReportCode("ZJ"+System.currentTimeMillis());
        return toAjax(checkReportService.save(checkReport));
    }

    /**
     * 修改检测报告信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CheckReport checkReport)
    {
        return success(checkReportService.saveOrUpdate(checkReport));
    }

    /**
     * 删除检测报告信息
     */
    @DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Integer[] reportIds)
    {
        return toAjax(checkReportService.removeBatchByIds(Arrays.asList(reportIds)));
    }

    @CrossOrigin
    @PostMapping("uploadFile")
    public String uploadFile(MultipartFile[] file){
        for (MultipartFile multipartFile : file) {
            String s = UploadMinioUtils.uploadPic(multipartFile);
            return s;
        }
        return "success";

    }

   /* @RequiresPermissions("crop:report:list")
    @GetMapping("/goodslist")
    public AjaxResult goodslist(Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        return success(list);
    }
    @RequiresPermissions("crop:report:list")
    @GetMapping("/materialLotlist")
    public AjaxResult materialLotlist(MaterialLot materialLot)
    {
        List<MaterialLot> list = materialLotService.listMaterialLot(materialLot);
        return success(list);
    }*/
    /*@Autowired
    private MinioUtils minioUtils;
    @RequestMapping("upload")
    public AjaxResult upload(MultipartFile file) throws Exception {
        String upload = minioUtils.upload(file);
        return success(upload);
    }*/
}
